package com.ruisoft.business.card.service;

import com.ruisoft.base.bean.QueryData;
import com.ruisoft.base.dao.BaseDao;
import com.ruisoft.base.service.BaseService;
import com.ruisoft.common.CommonUtil;
import com.ruisoft.core.param.CacheDataFactory;
import com.ruisoft.core.param.RequestUtil;

import org.json.JSONArray;
import org.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;


@Service
public class EmployeeCardService extends BaseService {

    @Autowired
    public BaseDao baseDao;
    
    public List<JSONObject> searchForPage(QueryData q) throws Exception {
        List<JSONObject> itemData = search(q, "employeeCard.select.employeeCardinfo.query");
        return itemData;
    }
    public List<JSONObject> searchCardInfoForPage(QueryData q) throws Exception {
        List<JSONObject> itemData = search(q, "employeeCard.select.employeeCardinfoAll.query");
        return itemData;
    }

    public JSONObject saveEmployeeCard(JSONObject jsonData) throws Exception {
        String cardid =(String)jsonData.get("cardid");
        JSONArray items = jsonData.getJSONArray("items");
        //产品明细
        JSONObject[] item = new JSONObject[items.length()];
        for (int i = 0; i < items.length(); i++) {
            item[i] = items.getJSONObject(i);
            item[i].put("cardid",cardid);
            //保存产品与属性值关联关系
            baseDao.add(item[i], "employeeCard.add.CardLicense.add");
        }
        baseDao.add(jsonData, "employeeCard.add.cardInfo.add");
        return jsonData;
    }
    /**
	 * 按照json格式查询库存信息
	 * 
	 * @param id
	 * @return
	 * @throws Exception
	 */
	public JSONObject searchEmployeeCard(String id) throws Exception {
		List<JSONObject> cardinfo = baseDao.queryId(id, "employeeCard.select.employeeCardinfo.query");
 			
		List<JSONObject> cardlicense = baseDao.queryId(id, "employeeCard.select.employeeCardlicense.query");
  
		JSONObject result=null;
		if(cardinfo.size()>0){
			result = cardinfo.get(0);
	        result.put("items", new JSONArray(cardlicense));
		}
		
        return result;
	}
	 /**
     * 删除员工卡信息
     * @param id
     * @return
     * @throws Exception
     */
    public int deleteCardInfo(String userid) throws Exception{
        int status = 0;
        status = baseDao.deleteCode(userid,"employeeCard.delete.cardLicense.delete");
        if(status >= 0) {
            status = baseDao.deleteCode(userid, "employeeCard.delete.cardInfo.delete");
        }
        return status;
    }
}
